wx.cloud.init()
const db = wx.cloud.database()
const test = db.collection('test')
Component({
  data: {
    date: "2000-01-01",
    region: ['浙江省', '杭州市', '江干区'],
    stringlength: 0,
    formData: {},
  },
  methods: {
    RegionChange: function (e) {
      this.setData({
        region: e.detail.value
      })
    },
    DateChange(e) {
      this.setData({
        date: e.detail.value
      })
    },
    formInputChange(e) {
      const {
        field
      } = e.currentTarget.dataset
      this.setData({
        [`formData.${field}`]: e.detail.value
      })
      console.log(this.data.formData)
    },
    introInput(e) {
      this.setData({
        intro: e.detail.value,
        stringlength: e.detail.value.length
      })
    },
    vcodeChange(e) {
      this.setData({
        vcode: e.detail.value
      })
    },
    submitForm: function (e) {
      console.log("submitform中的e:")
      console.log(e)
      console.log("formdata")
      console.log(this.data.formData)
      console.log("data")
      console.log(this.data)
      test.add({
        data: {
          username: this.data.formData.username,
          mobile: this.data.formData.mobile,
          vcode: this.data.vcode,
          date: this.data.date,
          signature: this.data.formData.signature,
          intro: this.data.intro
        },
        success: res => {
          // 在返回结果中会包含新创建的记录的 _id
          this.setData({
            username: e.detail.value.username
          })
          console.log("插入成功")
          wx.showToast({
            title: '新增记录成功',
          })
          console.log('[数据库] [新增记录] 成功，记录 _id: ', res._id)

        },
        fail: err => {
          console.log("插入失败")
          wx.showToast({
            icon: 'none',
            title: '新增记录失败'
          })
          console.error('[数据库] [新增记录] 失败：', err)
        }
      })

    },
    getVCode: function (e) {}

  }
});